package config

import (
	"flag"
	"gopkg.in/yaml.v2"
	"os"
)

// 启动命令需要指定配置文件路径，否则就是相对路径的config目录下
var configFile = flag.String("config", "config/app.yaml", "-config path")

var Cfg *Config

type Config struct {
	Redis   RedisConfig   `yaml:"redis"`
	Sqlite  SqliteConfig  `yaml:"sqlite"`
	Jwt     JwtConfig     `yaml:"jwt"`
	Storage StorageConfig `yaml:"storage"`
}

type StorageConfig struct {
	Path string `yaml:"path"`
}

type SqliteConfig struct {
	DbPath string `yaml:"dbPath"`
}

type JwtConfig struct {
	SignKey string `yaml:"signKey"`
}

type RedisConfig struct {
	Host string `yaml:"host"`
	Port int    `yaml:"port"`
}

func Parse() (*Config, error) {
	config := &Config{}

	file, err := os.Open(*configFile)
	if err != nil {
		return nil, err
	}
	defer file.Close()

	if err := yaml.NewDecoder(file).Decode(&config); err != nil {
		return nil, err
	}

	Cfg = config

	return config, nil
}
